<template>
  <DetailsFormBox :title="page.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      hidden: !isEdit,
      click: getForm,
    },
    {
      title: '通过',
      icon: '',
      type: 'success',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form, false)
      },
    },
    {
      title: '拒绝',
      icon: '',
      type: 'danger',
      hidden: !isEdit,
      isNotRule: true,
      click: (form: any) => {
        proxy.$messageInputBox('请填写拒绝原因！', (value: string) => {
          proxy.$api.ProductionBusniess.RefuseProductHandInBill({
            handInBillNo: form.associationBillNo,
            inStockRefuseReason: value,
          })
            .then((res: any) => {
              proxy.$message('拒绝成功');
              closeBtn();
            })
            .catch(() => { });
        });
      },
    },
  ]" :formInfo="[
    {
      label: '创建人员',
      value: 'createPersonName',
    },
    {
      label: '创建时间',
      value: 'createTime',
    },
  ]" :loading="data.loading" :loadingMsg="data.loadingMsg">
    <template #default="{ form }">
      <div class="p-12">
        <OrderInfo :produceTaskBillNo="form.produceTaskBillCode" />
        <CellBox title="基本信息" isTop>
          <FlexBox>
            <ElFormItemBox label="单据单号" prop="inStockBillNo">
              <ElInputBox :data="form" dataName="inStockBillNo" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="入库类型" prop="inStockType">
              <ElSelectBox :data="form" dataName="inStockType" :options="[
                {
                  label: '成品入库',
                  value: 1,
                },
                {
                  label: '半成品入库',
                  value: 2,
                }
              ]" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="关联单据" prop="associationBillNo">
              <ElInputBox :data="form" dataName="associationBillNo" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="单据备注" prop="inStockBillRemark" class="wAuto">
              <ElInputBox :data="form" dataName="inStockBillRemark" type="textarea" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="拒绝原因" prop="disabledReason" class="wAuto" v-if="!!form.disabledReason">
              <ElInputBox :data="form" dataName="disabledReason" type="textarea" disabled />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
        <CellBox title="单据明细" isTop>
          <TableBoxMx :data="{
            disabled: !isEdit,
            tbody: form.detailList,
            thead: [
              {
                label: '缴库类型',
                prop: 'handInType',
                align: 'center',
                width: 120,
                options: options.handInTypeMx
              },
              {
                label: '产品型号',
                prop: 'productSpecificationName',
                align: 'center',
                width: 160,
              },
              {
                label: '生产批次',
                prop: 'produceTaskBillNo',
                align: 'center',
                width: 140,
              },
              {
                label: '生产日期',
                prop: 'productionDate',
                align: 'center',
                width: 160,
                formType: 'day',
                formProp: 'detailList',
              },
              {
                label: '入库数量',
                prop: 'handInQuantity',
                align: 'center',
                width: 120,
              },
              {
                label: '库区库位',
                prop: 'areaShelfCode',
                align: 'center',
                formType: 'cascader',
                formProp: 'detailList',
                dataName: ['warehouseAreaCode', 'areaShelfCode'],
                options: options.warehouse,
                width: 160,
              },
              {
                label: '备注',
                prop: 'handInMxRemark',
                align: 'left',
                width: null,
                formType: 'text'
              },
            ],
          }">
            <template #productionDate="{ row }" v-if="!isEdit">
              {{ proxy.$day(row.productionDate) }}
            </template>
            <template #areaShelfCode="{ row }" v-if="!isEdit">
              {{ `${row.warehouseAreaName}/${row.areaShelfName}` }}
            </template>
          </TableBoxMx>
        </CellBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { sendMsg } from '@/utils/crossTagMag.ts';
import { useListComponent } from '../pageTs.ts'; // 引入逻辑
const { dataInfo, options, getPageProductHandInBills } = useListComponent();
import page from '../page.ts'; // 引入逻辑
import '../pageCss.scss'; // 引入样式
const { proxy } = getCurrentInstance() as any;
const data: any = reactive({
  loading: false,
  loadingMsg: '',
  mainPage: 1,
  form: {
    handInBillNo: "", // 	缴库单号（关联单据号)
    inStockBillRemark: "", // 作废原因
    detailList: []
  },
});
const isEdit = ref(true)

import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
//获取数据
const getForm = async () => {
  const _query: any = route.query
  setLoading()
  //是否新建
  if (!!!_query.code) {
    data.loading = false
    getAssociationBillNo()
    return
  }
  proxy.$api.ProductInStock.GetSingleProductInStockBill({
    queryBy: 1,
    queryValue: _query.code,
  })
    .then((res: any) => {
      proxy.$message('获取成功')
      isEdit.value = ['1', '2'].includes(_query.type)
      data.form = res
      data.loading = false
    })
    .catch(() => {
      data.loading = false
    });
};
//获取关联单号信息
const getAssociationBillNo = () => {
  const associationBillNo: any = route.query.associationBillNo
  const form: any = data.form
  if (associationBillNo) {
    getPageProductHandInBills(associationBillNo, (res: any) => {
      form.associationBillNo = res.handInBillNo
      form.produceTaskBillCode = res.produceTaskBillCode
      form.inStockType = res.handInType
      form.handInRemark = res.handInRemark
      form.detailList = res.detailList.map((el: any) => ({
        handInBillMxNo: el.handInBillMxNo,//缴库明细唯一编码
        handInType: el.handInType,//缴库类型：0-发货缴库；1-库存缴库
        produceTaskBillNo: res.produceTaskBillCode,//生产单号
        productSpecificationName: el.productSpecificationName,//成品型号名称
        productNo: el.productSpecificationCode,//成品编号
        packageGuiGeNo: el.packageGuiGeNo,//包装规格编码
        packageDescription: el.packageDescription,//包装描述
        handInBoxs: el.handInBoxs,//入库箱数（缴库箱数）
        handInQuantity: el.handInQuantity,//缴库数量（实际数量、入库数量）
        eachBoxQuantity: el.eachBoxQuantity,//每箱数量
        kilogramQuantity: el.kilogramQuantity,//公斤数
        warehouseAreaCode: '',//库区代码
        areaShelfCode: '',//库位代码
        handInMxRemark: el.handInMxRemark,//入库备注（默认缴库备注带过来，可以修改后提交）
      }))
    })
  }
}
const saveBtn = (form: any, isConfirm: boolean = false, isPartial?: number) => {
  const _form: any = {
    handInBillNo: form.associationBillNo, // 缴库单号（关联单据号)
    inStockBillRemark: form.inStockBillRemark, // 缴库单号（关联单据号)
    inStockBillMxList: form.detailList.map((el: any) => (
      {
        handInBoxs: el.handInBoxs, // 	入库箱数（缴库箱数）
        handInQuantity: el.handInQuantity, // 缴库数量（实际数量、入库数量）
        eachBoxQuantity: el.eachBoxQuantity, //每箱数量
        kilogramQuantity: el.kilogramQuantity, //公斤数
        warehouseAreaCode: el.warehouseAreaCode, //库区代码
        areaShelfCode: el.areaShelfCode, //库区代码
        handInMxRemark: el.handInMxRemark, //入库备注（默认缴库备注带过来，可以修改后提交）
        productNo: el.productNo, //成品规格编号
        packageGuiGeNo: el.packageGuiGeNo, //包装规格编码
        packageDescription: el.packageDescription, //包装描述
        handInBillMxNo: el.handInBillMxNo, //缴库明细唯一编码
        produceTaskBillNo: el.produceTaskBillNo, //生产单号
        handInType: el.handInType, //缴库类型：0-发货缴库；1-库存缴库
      }
    ))
  }
  // return
  switch (route.query.type) {
    case '1'://新建
      proxy.$api.ProductInStock.CreateProductInStockBill(_form)
        .then((res: any) => {
          if (isConfirm) {

          } else {
            _sun()
          }
        })
        .catch(() => { });
      break;
    default:
      break;
  }
  function _sun() {
    closeBtn('保存成功')
  }
};
const closeBtn = (message?: string) => {
  if (message) {
    proxy.$message(message)
  }
  sendMsg('save', true);
  // getForm()
  window.close();
};
const setLoading = (title: string = '正在获取数据,请稍等...') => {
  if (!data.loading) {
    data.loading = true;
    data.loadingMsg = title;
  }
};
import { getWarehouse } from '@/utils/optionsArr.ts';
onMounted(() => {
  getForm();
  getWarehouse(options, 'warehouse')//获取仓库
});
</script>
